﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using CSScriptLibrary;


/*
 * Project Blackbird - The revival
 * Copyright <C> 2012 - Adil Hafeez
 * Licensed under the Ms-Pl OSS license. <http://www.opensource.org/licenses/ms-pl>
 * http://mmoscene.net
 * Credits:
 * Zak - General help
 * Matty - Socket system
 */

/*
 * Credits for this file:
 * AJ - Jolt (CS-Script idea thingamajig)
 */
namespace Blackbird.Utilities.Plugin
{
    /// <summary>
    /// Manages CS-Script plugins.
    /// </summary>
    class ScriptManager
    {
        /// <summary>
        /// Dictionary which holds all known scripts.
        /// </summary>
        private Dictionary<string, dynamic> Scripts;

        /// <summary>
        /// Holds information about the directory where scripts are.
        /// </summary>
        private DirectoryInfo ScriptDirectory;

        /// <summary>
        /// The path to the scripts, specified as a string object.
        /// </summary>
        private string ScriptPath;

        /// <summary>
        /// The logging instance of the class.
        /// </summary>
        private Log log;


        /// <summary>
        /// Initialise the class.
        /// </summary>
        /// <param name="ScriptPath">The path where scripts are located.</param>
        

        public ScriptManager(string ScriptPath)
        {
            this.ScriptPath = ScriptPath;
            this.ScriptDirectory = new DirectoryInfo(this.ScriptPath);
            this.log = new Log("PluginManager");
            this.Scripts = new Dictionary<string, dynamic>();
        }

        /// <summary>
        /// Initialise the plugins.
        /// </summary>
        public void InitializePlugins()
        {
            log.Debug("Attempting to boot plugins...");
            if (this.ScriptDirectory.Exists)
            {
                foreach (FileInfo FI in this.ScriptDirectory.GetFiles()) //foreach through each file
                {
                    if (FI.Exists) //check if file(s) exist(s)
                    {
                        var ScriptFile = new AsmHelper(CSScript.Load(FI.FullName)); //dynamic assembly loading..

                        try
                        {
                            using (StreamReader Reader = new StreamReader(FI.Open(FileMode.Open))) //read through the file
                            {
                                ScriptFile.Invoke("*.Main"); //Invoke the Main() method, with the wildcard (any main method invoked).
                                
                                this.Scripts.Add(FI.Name, ScriptFile); //add to dictionary




                            }
                        }
                        catch (Exception e)
                        {
                            log.Fatal(e.Message); //something went wrong
                            
                        }
                    }

                }
                log.Info(this.Scripts.Count() + " plugin(s) loaded.");
                
                
            }
            else
            {
                log.Warn("Plugin directory was not found!");
            }

           
        }

       

    }
}
